import { createRouter, createWebHashHistory } from "vue-router";
import { Toast } from "vant";

const routes = [
  {
    path: "/goodsInfo",
    name: "GoodsInfo",
    component: () =>
      import(
        /* webpackChunkName: "goodsInfo" */ "../views/goodsInfo/index.vue"
      ),
  },
  {
    path: "/login",
    name: "Login",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/login/index.vue"),
  },
  {
    path: "/home",
    name: "Home",
    component: () =>
      import(/* webpackChunkName: "home" */ "../views/home/index.vue"),
    children: [
      {
        path: "index",
        name: "Index",
        component: () =>
          import(/* webpackChunkName: "index" */ "../views/index/index.vue"),
      },
      {
        path: "goods",
        name: "Goods",
        component: () =>
          import(/* webpackChunkName: "goods" */ "../views/goods/index.vue"),
      },
      {
        path: "news",
        name: "News",
        component: () =>
          import(/* webpackChunkName: "news" */ "../views/news/index.vue"),
      },
      {
        path: "my",
        name: "My",
        component: () =>
          import(/* webpackChunkName: "my" */ "../views/my/index.vue"),
      },
    ],
  },
  {
    path: "/orderDetails",
    name: "OrderDetails",
    component: () =>
      import(
        /* webpackChunkName: "orderDetails" */ "../views/orderDetails/index.vue"
      ),
  },
  {
    path: "/setUp",
    name: "setUp",
    component: () =>
      import(/* webpackChunkName: "setUp" */ "../views/my/setUp/index.vue"),
  },
  {
    path: "/profit",
    name: "profit",
    component: () =>
      import(/* webpackChunkName: "profit" */ "../views/my/profit/index.vue"),
  },
  {
    path: "/bankCard",
    name: "bankCard",
    component: () =>
      import(
        /* webpackChunkName: "bankCard" */ "../views/my/profit/bankCard/index.vue"
      ),
  },
  {
    path: "/myGoods",
    name: "myGoods",
    component: () =>
      import(/* webpackChunkName: "myGoods" */ "../views/my/myGoods/index.vue"),
  },
  {
    path: "/purchase",
    name: "purchase",
    component: () =>
      import(
        /* webpackChunkName: "purchase" */ "../views/my/purchase/index.vue"
      ),
  },
  {
    path: "/myGoodsInfo",
    name: "myGoodsInfo",
    component: () =>
      import(
        /* webpackChunkName: "myGoodsInfo" */ "../views/myGoods/myGoodsInfo/index.vue"
      ),
  },
  {
    path: "/MyGoodsTrade",
    name: "MyGoodsTrade",
    component: () =>
      import(
        /* webpackChunkName: "myGoodsTrade" */ "../views/myGoods/myGoodsTrade/index.vue"
      ),
  },
  {
    path: "/collectPayMoney",
    name: "CollectPayMoney",
    component: () =>
      import(
        /* webpackChunkName: "collectPayMoney" */ "../views/my/collectPayMoney/index.vue"
      ),
  },
  {
    path: "/team",
    name: "Team",
    component: () =>
      import(/* webpackChunkName: "team" */ "../views/my/team/index.vue"),
  },
  {
    path: "/address",
    name: "Address",
    component: () =>
      import(/* webpackChunkName: "address" */ "../views/my/address/index.vue"),
  },
  {
    path: "/addressSon",
    name: "AddressSon",
    component: () =>
      import(
        /* webpackChunkName: "addressSon" */ "../views/my/address/components/index.vue"
      ),
  },
  {
    path: "/notice",
    name: "Notice",
    component: () =>
      import(/* webpackChunkName: "notice" */ "../views/my/notice/index.vue"),
  },
  {
    path: "/agreement",
    name: "Agreement",
    component: () =>
      import(
        /* webpackChunkName: "agreement" */ "../views/my/notice/components/agreement.vue"
      ),
  },
  {
    path: "/poster",
    name: "Poster",
    component: () =>
      import(/* webpackChunkName: "poster" */ "../views/my/poster/index.vue"),
  },
  {
    path: "/",
    redirect: "/home/index",
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem("token") ? true : false;
  if (to.name !== "Login" && !isAuthenticated) {
    console.log("to---", to);
    if (to.name === "Notice" || to.name === "Agreement") return next();
    next({ name: "Login" });
    Toast.fail("请先登录");
  } else next();
});

export default router;
